#include "stc15f2k60s2.h"
#include "Seg.h"
#include "stdio.h"
#include "Tim.h"
#include "onewire.h"
#include "Key.h"
#include "LED.h"
unsigned char pucSeg_Buf[9],pucSeg_Code[8],ucSeg_Pos;
unsigned int ucSeg_Dly,uiTemp;
unsigned char ucKey_Dly,ucKey_Old;
unsigned long ulms;
unsigned char ucState=1,ucSec;
void Seg_Proc(void);
void Key_Proc(void) ;
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	while(1){
		Seg_Proc();
		Key_Proc() ;
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
		ulms++; 
	if(++ucKey_Dly==20) ucKey_Dly=0;
	 if(!(ulms % 1000)) 
	 { 
		 ucSec++;
	 } 
	 if(++ucSeg_Dly>=500) ucSeg_Dly=0;
	 Seg_Disp(pucSeg_Code,ucSeg_Pos) ;
	 if(++ucSeg_Pos>=8) ucSeg_Pos=0;
} 
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 if(ucKey_Down==4)  { 
		 if(++ucState==2) ucState=0;
	 } 
		Led_Disp(ucState+1) ;
}
void Seg_Proc(void){
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	switch(ucState){
		case 0:
			sprintf(pucSeg_Buf,"1 %06d",(unsigned int)ucSec);
		break;
		case 1:
			uiTemp=rd_temperature();
			sprintf(pucSeg_Buf,"2 %04.2f",uiTemp/16.0);
			break;
	}
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}